<template>
  <div class="trend-pop-content" @click.stop id="wrapper" :class="{'isHorizontal':!isHorizontal}">
    <div class="wrap">
      <div class="flex-between">
        <p class="title">近30天热词</p>
        <div class="flex-center">
          <img class="rotateScreen" @click="handleRotate" v-if="isHorizontal" src="@/assets/event/icon-screen-change2.png" alt="">
          <img class="rotateScreen" @click="handleRotate" v-else src="@/assets/event/icon-screen-change.png" alt="">
          <img class="close" @click="closePop" src="@/assets/event/icon-close.png" alt="">
        </div>
      </div>
      <ul class="trend-tabs">
        <li v-for="(item,index) in props.words" :key="index" @click="wordChange(item.words)" :class="{'active': wordSelect == item.words}">{{item.words}}</li>
      </ul>
      <div class="trend-chart" id="trendChart" ref="refTrendChart"></div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { computed, ref, reactive, toRefs, watch, onActivated, onMounted } from 'vue'
import { hotWordLineChart } from "./echart.js"
import TypeFilter from '@/views/keyWork/components/typeFilter.vue'
import EventServe from '@/api/event'

defineOptions({
  name: 'TrendPop'
})
const emit = defineEmits(['close'])
const props = defineProps({
  wordType: {
    type: String,
    default: ''
  },
  words: {
    type: Array,
    default: []
  }
})

const wordSelect = ref(0)
const wordChange = (val) => {
  wordSelect.value = val
  EventServe.getTrendChart({
    "type": props.wordType,
    "word": wordSelect.value
  }).then(res => {
    hotWordLineChart("trendChart",res.data.trendTable)
  })
}

onMounted(()=>{
  console.log(props.words)
  wordSelect.value = props.words[0].words
  wordChange(wordSelect.value)
})

const closePop = () => {
  emit("close")
}

//横屏，将画面宣旋转90度
const isHorizontal = ref(true)
const handleRotate = () => {
  isHorizontal.value = !isHorizontal.value
  detectOrient()
  wordChange(wordSelect.value)
}

const refTrendChart = ref<HTMLElement | null>(null)
const detectOrient = () => {
  var width = document.documentElement.clientWidth
  var height = document.documentElement.clientHeight
  var $wrapper = document.getElementById("wrapper")

  let style = "";
  if (isHorizontal.value) { // 横屏
    style += "-webkit-transform: rotate(0) translate(0,-50%); transform: rotate(0) translate(0,-50%);";
    style += "-webkit-transform-origin: 0 0;";
    style += "transform-origin: 0 0;";
  }
  else { // 竖屏
    style += "-webkit-transform: rotate(90deg) translate(0,0); transform: rotate(90deg) translate(0,0);";
    style += "-webkit-transform-origin: " + width / 2 + "px " + width / 2 + "px;";
    style += "transform-origin: " + width / 2 + "px " + width / 2 + "px;";
  }
  $wrapper.style.cssText = style;
}
</script>

<script lang="ts">

</script>

<style lang="less" scoped>
.trend-pop-content{
  background: #fff;
  position: absolute;
  top: 50%;
  transform: rotate(0deg) translate(0,-50%);
  width: 100vw;
  z-index: 99;
  .wrap{
    padding: 15px 16px;
  }
  .title{
    font-size: 15px;
    color: #212121;
  }
  .rotateScreen{
    width: 20px;
    height: 20px;
    margin-right: 20px;
  }
  .close{
    width: 15px;
    height: 15px;
  }
  .trend-chart{
    width: 100%;
    height: 200px;
  }
  .trend-tabs{
    display: flex;
    flex-wrap: wrap;
    padding-top: 5px;
    li{
      padding: 0 16px;
      height: 30px;
      line-height: 30px;
      border: 1px solid #F4F8FB;
      border-radius: 5px;
      background: #F4F8FB;
      margin-right: 10px;
      margin-top: 10px;
      color: #000000;
      font-size: 12px;

      &.active {
        border-radius: 5px;
        background: #F4F8FB;
        border: 1px solid #3D76CC;
        color: #3D76CC;
      }
      &:last-child{
        margin-right: 0;
      }
    }
  }
  &.isHorizontal{
    top: 5vh;
    width: 90vh;
    height: 100vw;
    .trend-chart{
      width: 100%;
      height: calc(100vw - 100px);
    }
  }
}
</style>

